//
//  MonitorBMKCustomAnnotationView.swift
//  TJJPT
//
//  Created by FENGYL on 2017/7/25.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

import UIKit

class MonitorBMKCustomAnnotationView: BMKAnnotationView {
    
    private var calloutView:MonitorCustomCalloutView?
    
    var serialNumber:String?
    var time:String?
    var displayName:String?
    
    let kCalloutWidth = 200.0
    let kCalloutHeight = 90.0
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        
//        super.setSelected(selected, animated: animated)
        
//        if (self.isSelected == selected)
//        {
//            return;
//        }
        
        if (selected)
        {
            if (self.calloutView == nil)
            {
                
                let labelString = NSString(string: serialNumber ?? "")
                let labelWidth = labelString.boundingRect(with: CGSize(width: (STATIC_VALUE_SCREEN_WIDTH - 20), height: 20), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 13.0)], context: nil)
                
                let timeString = NSString(string: time ?? "")
                let timeWidth = timeString.boundingRect(with: CGSize(width: (STATIC_VALUE_SCREEN_WIDTH - 20), height: 20), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 13.0)], context: nil)
                
                let displayNameString = NSString(string: displayName ?? "")
                let displayNameWidth = displayNameString.boundingRect(with: CGSize(width: (STATIC_VALUE_SCREEN_WIDTH - 20), height: 20), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 13.0)], context: nil)
                
                let array = [Double(labelWidth.width),Double(timeWidth.width),Double(displayNameWidth.width),kCalloutWidth]
                
                var width = kCalloutWidth
                for k in array {
                    
                    if k > width {
                        
                        width = k
                        
                    }
                    
                }
                width += 20
                
                
                self.calloutView = MonitorCustomCalloutView.init(frame: CGRect(x: 0, y: 0, width: width, height: kCalloutHeight))
                self.calloutView?.center = CGPoint(x: (self.bounds.width / 2.0 + (self.calloutView?.x)!), y: -((self.calloutView?.bounds.height)! / 2.0 + self.calloutOffset.y))
            }
            
            
            self.calloutView?.setUpLabel(serialNumber: serialNumber ?? (NSLocalizedString(LOCALIZABLE_STRING_DEVICE_SERIAL_NUMBER, comment: "") + ":"), time: time ?? (NSLocalizedString(LOCALIZABLE_STRING_LAST_CONNECTED, comment: "") + ":"), displayName: displayName ?? (NSLocalizedString(LOCALIZABLE_STRING_DISPLAY_NAME, comment: "") + ":"))
            
            self.addSubview(self.calloutView!)
        }
        else
        {
            self.calloutView?.removeFromSuperview()
        }
        
        //        super.setSelected(selected, animated: animated)
        
        
        
        
    }

    
    

}
